<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\common\model\User;

/**
 * 手机短信接口
 */
class Sms extends Api
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 发送验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     */
    public function send()
    {
        $mobile = $this->request->request("mobile");
        $event = $this->request->request("event");
        $action = $this->request->request("action");
        $event = $event ? $event : 'default';

        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        switch ($event) {
            case 'chinese':
                 $activity = new \app\admin\model\activtyc\Chinese;
                 if($activity->where('phone','=',$mobile)->count()){
                        $this->error('已经领取,不可重复领取。');
                 }
                break;
            case 'guessing':
                if($action){
                    $Guessing = new \app\admin\model\activtyc\Guessing;
                    $where = ['phone'=>$mobile,'pay'=>1,'contract'=>$action];
                    if($Guessing->where($where)->count()){
                        $this->error('已经领取,不可重复领取。');
                    }
                }
                break;
            case 'examiation':
                $activity = new \app\admin\model\activtyc\Examiation;
                if($activity->where('phone','=',$mobile)->count()){
                    $this->error('已经领取,不可重复领取。');
                }
                break;
            case 'teacher':
                $teacher = new \app\admin\model\activity\Teacher;
                if($teacher->where('tel','=',$mobile)->count()){
                    $this->error('已经领取,不可重复领取。');
                }
                break;
            case 'schoolques':
                $Round = new \app\admin\model\activtyc\Round;
                if($Round->where('phone','=',$mobile)->count()){
                    $this->error('已经领取,不可重复领取。');
                }
                break;
            case 'schoolxiaoxue':
                $Xiaoxue =  new \app\admin\model\activity\Xiaoxue;
                if($Xiaoxue->where('phone','=',$mobile)->count()){
                    $this->error('已经领取,不可重复领取。');
                }
                break;
            default:
                # code...
                break;
        }

        $last = Smslib::get($mobile,$event);
       
        if (!$last) {
            $this->error(__('请120秒之后重新获取'));
        }
        // $ipSendTotal = \app\common\model\Sms::where(['ip' => $this->request->ip()])->whereTime('createtime', '-1 hours')->count();
        // if ($ipSendTotal >= 5) {
        //     $this->error(__('发送频繁'));
        // }
        $ret = Smslib::send($mobile, NULL,$event);

        if ($ret['result'] == 0) {
            $this->success($ret['desc']);
        } else {
            $this->error('发送失败');
            //$this->error($ret['desc']);
        }
    }

    /**
     * 检测验证码
     *
     * @param string $mobile 手机号
     * @param string $event 事件名称
     * @param string $captcha 验证码
     */
    public function check()
    {
        $mobile = $this->request->request("mobile");
        $captcha = $this->request->request("captcha");
        $event = $this->request->request("event");
        $event = $event ? $event : 'default';
        if (!$mobile || !\think\Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__('手机号不正确'));
        }
        $ret = Smslib::check($mobile, $captcha,$event);
        if ($ret) {
            $this->success(__('成功'));
        } else {
            $this->error(__('验证码不正确'));
        }
    }

}
